#!/usr/bin/env python3
# -*-coding:utf-8 -*-
# __author__=LuLu
from time import sleep

from comment.Tools import is_contains_chinese
from page import basePage
from pagesLocators.memberLocators import MemberLoc as Loc


class Member(basePage.BasePage):
    # 选择查询开始时间
    def select_start_time(self, start_date):
        # 解除readonly，采用输入方式，格式按照 YYYY-MM-DD
        self.remove_execute_script('readonly',
                                   Loc.startTime)
        self.clear_and_sendKey_element(Loc.startTime, start_date)

    # 选择查询结束时间
    def select_end_time(self, end_date):
        # 解除readonly，采用输入方式，格式按照 YYYY-MM-DD
        self.remove_execute_script('readonly',
                                   Loc.endTime)
        self.clear_and_sendKey_element(Loc.endTime, end_date)

    # 键入会员编号
    def send_key_account_number(self, account_number):
        self.clear_and_sendKey_element(Loc.accountNumber, account_number)

    # 键入会员昵称
    def send_key_nickname(self, nickname):
        self.clear_and_sendKey_element(Loc.nickName, nickname)

    # 键入手机号码
    def send_mobile(self, mobile):
        self.clear_and_sendKey_element(Loc.mobile, mobile)

    # 选择性别
    def select_sex(self, sex='女'):
        self.clear_and_sendKey_element(Loc.sex, sex)
        if sex == '全部':
            self.remove_execute_script('data-value', Loc.sex)
        elif sex == '未知':
            self.set_value_execute_script('data-value', '0', Loc.sex)
        elif sex == '男':
            self.set_value_execute_script('data-value', '1', Loc.sex)
        elif sex == '女':
            self.set_value_execute_script('data-value', '2', Loc.sex)

        # 非标准select控件，无法运用Select方法定位选择元素
        # self.click_element(Loc.sex)
        # ss=Select(Loc.sex)
        # ss.select_by_visible_text(sex)

    # 选择会员角色 -暂时用不到
    # def select_accountManagerType(self, accountManagerType='全部'):
    #     # self.sendkey_element(Loc.accountManagerType,nickname)
    #     if accountManagerType == '全部':
    #         pass
    #     if accountManagerType == '普通会员':
    #         self.set_value_execute_script('data-value=3_3', Loc.accountManagerType)
    #     elif accountManagerType == '分销管理员':
    #         self.set_value_execute_script('data-value=2_2', Loc.accountManagerType)
    #     elif accountManagerType == '分销员':
    #         self.set_value_execute_script('data-value=2_3', Loc.accountManagerType)

    # 选择会员状态
    def select_status(self, status='全部'):
        self.clear_and_sendKey_element(Loc.status, status)
        if status == '全部':
            self.remove_execute_script('data-value', Loc.status)
        elif status == '正常':
            self.set_value_execute_script('data-value', '1', Loc.status)

    # 选择注册来源
    def select_registerSourceType(self, registerSourceType):
        self.clear_and_sendKey_element(Loc.registerSourceType, registerSourceType)
        if registerSourceType == '全部':
            self.remove_execute_script('data-value', Loc.registerSourceType)
        elif registerSourceType == '直接注册':
            self.set_value_execute_script('data-value', '1', Loc.registerSourceType)
        elif registerSourceType == '分销注册':
            self.set_value_execute_script('data-value', '3', Loc.registerSourceType)
        elif registerSourceType == '邀请注册':
            self.set_value_execute_script('data-value', '2', Loc.registerSourceType)

    # 键入邀请公司
    def sendkey_inviteCompanyFullName(self, inviteCompanyFullName):
        self.clear_and_sendKey_element(Loc.inviteCompanyFullName, inviteCompanyFullName)

    # 键入邀请人
    def sendkey_inviteNickName(self, inviteNickName):
        self.clear_and_sendKey_element(Loc.inviteNickName, inviteNickName)

    # 点击搜索按钮
    def click_search(self):
        self.click_element(Loc.SearchButton)
        sleep(1)

    # 点击清空按钮
    def click_searchClean(self):
        self.click_element(Loc.searchCleanButton)

    # # 判断数据数量，数量超过则判断是否存在翻页控件
    # def page_num(self):
    #     if self.isElementExist(Loc.PageNum):
    #         str = self.get_element_value(Loc.PageNum)
    #         if is_contains_chinese(str):
    #             return int(str.replace(f'显示第 1 到第 {len(self.getTableRow(Loc.memberTable))} 条记录，总共', '').
    #                        replace('条记录', '').strip())
    #         else:
    #             # 加载为英文时
    #             return int(str.replace(f'Showing 1 to {len(self.getTableRow(Loc.memberTable))} of', '').
    #                        replace('rows per page', '').strip())
    #     else:
    #         print("会员页面数据<10,不存在翻页")
    #         return None
